//给定一个数组 nums 和滑动窗口的大小 k，请找出所有滑动窗口里的最大值。
#include <iostream>
#include <vector>
#include <deque>
using namespace std;
class Solution
{
public:
    vector<int> maxSlidingWindow(vector<int> &nums, int k)
    {
        vector<int> ans;
        deque<int> d;
        if (k == 0 || nums.size() == 0)
        {
            return ans;
        }
        for (int i = 0; i < k; i++)
        {
            while (!d.empty() && nums[d.back()] < nums[i])
            {
                d.pop_back();
            }
            d.push_back(i);
        }
        ans.push_back(nums[d.front()]);
        for (int i = 0; i < nums.size() - k; i++)
        {
            while(!d.empty() && d.front() <= i){
                d.pop_front();
            }
            while (!d.empty() && nums[d.back()] < nums[i+k])
            {
                d.pop_back();
            }
            d.push_back(i+k);
            ans.push_back(nums[d.front()]);
        }

        return ans;
    }
};

int main(int argc, char const *argv[])
{
    Solution s;
    vector<int> nums = {9, 10, 9, -7, -4, -8, 2, -6};
    vector<int> ans = s.maxSlidingWindow(nums, 5);
    for (int i = 0; i < ans.size(); i++)
    {
        cout << ans[i] << " ";
    }
    cout << endl;
    return 0;
}